<?php
namespace Functions\Controller;
use Admin\Controller\AdminController;
/**
 * 分店管理
 */

class StoreController extends AdminController {
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '分店管理',
                'description' => '管理所有分店',
                ),
            'menu' => array(
                array('name' => '分店列表',
                    'url' => U('Functions/Store/index'),
                    'icon' => 'list',
                    ),
                ),
            'add' => array(
                array('name' => '添加分店',
                    'url' => U('add'),
                    ),
                ),
            );
    }
	/**
     * 套系类型
     */
    public function index(){

        //筛选条件
        $where = array();
        $keyword = trim(I('request.keyword',''));
        if(!empty($keyword)){
            $where['_string'] = ' (store_name like "%'.$keyword.'%") OR ( store_tel like "%'.$keyword.'%") ';
        }
        $storeId = I('request.store_id','');
        if(!empty($storeId)){
            $where['store_id'] = $storeId;
        }

        $breadCrumb = array('分店列表' => U());
        $this->assign('breadCrumb', $breadCrumb);
        $model = D('Functions/Store');
        //URL参数
        $pageMaps = array();
        //查询数据
        $count = $model->countList($where);
        $limit = $this->getPageLimit($count,10);
        $storeSearch = $model -> loadList();//店铺树结构
        $storeList = $model -> loadData($where,$limit);
        $this->assign('storeSearch', $storeSearch);
        $this->assign('storeList', $storeList);
        $this->assign('page',$this->getPageShow($pageMaps));
        $this->assign('storeId',$storeId);
        $this->adminDisplay();
    }

    /**
     * 增加
     */
    public function add()
    {
        if (!IS_POST)
        {
            $breadCrumb = array('分店管理' => U('index'), '分店添加' => U());
            $this->assign('breadCrumb', $breadCrumb);
            $this->assign('name', '添加');
            $model = D('Functions/Store');
            $storeList = $model ->loadList();
            $this->assign('storeList', $storeList);
            $this->adminDisplay('info');
        }
        else
        {
            $model = D('Functions/Store');
            if ($model->saveData('add'))
            {
                $this->success('分店添加成功！');
            }
            else
            {
                $msg = $model->getError();
                if (empty($msg))
                {
                    $this->error('分店添加失败');
                }
                else
                {
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 修改
     */
    public function edit()
    {
        $model = D('Functions/Store');
        if (!IS_POST)
        {
            $storeId = I('get.store_id', '', 'intval');
            if (empty($storeId))
            {
                $this->error('参数不能为空！');
            }
            $info = $model->getInfo($storeId);
            if (!$info)
            {
                $this->error($model->getError());
            }
            $breadCrumb = array('分店列表' => U('index'), '分店修改' => U('edit', array('store_id' => $storeId)));
            $this->assign('breadCrumb', $breadCrumb);
            $this->assign('name', '修改');
            $this->assign('info', $info);
            $storeList = $model ->loadList();
            $this->assign('storeList', $storeList);
            $this->adminDisplay('info');
        }
        else
        {

            if ($model->saveData('edit'))
            {
                $this->success('分店修改成功！');
            }
            else
            {
                $msg = $model->getError();
                if (empty($msg))
                {
                    $this->error('分店修改失败');
                }
                else
                {
                    $this->error($msg);
                }
            }
        }
    }

  /**
     * 删除
     */
    public function del()
    {
        $StoreId = I('post.data');
        if (empty($StoreId))
        {
            $this->error('参数不能为空！');
        } 
        // 删除操作
        $model = D('Functions/Store');
        //判断子栏目
        if($model->loadList('', $StoreId)){
            $this->error('请先删除子分店！');
        }
        
        if ($model->delData($StoreId))
        {
            $this->success('分店删除成功！');
        }
        else
        {
            $msg = $model->getError();
            if (empty($msg))
            {
                $this->error('分店删除失败！');
            }
            else
            {
                $this->error($msg);
            }
        }
    }

}

